#include "ReShade.fxh"

// =====================================================
// Scanline_CA.fx
// Otimizado para Resolução Nativa 960x540
// =====================================================

uniform bool EnableScanlines <
    ui_label = "Enable Horiz Scanlines";
> = true;

uniform float Darkness <
    ui_label = "Scanline Darkness";
    ui_type = "slider";
    ui_min = 0.0; ui_max = 1.0; ui_step = 0.05;
> = 0.1;

uniform float CA_Amount <
    ui_label = "Chrom. Aberration";
    ui_type = "slider";
    ui_min = 0.0; ui_max = 10.0; ui_step = 0.1;
> = 1.0;

uniform float Brightness <
    ui_label = "Brightness";
    ui_type = "slider";
    ui_min = -1.0; ui_max = 1.0; ui_step = 0.05;
> = 0.0;

uniform float Contrast <
    ui_label = "Contrast";
    ui_type = "slider";
    ui_min = 0.0; ui_max = 2.0; ui_step = 0.05;
> = 1.0;

uniform float Saturation <
    ui_label = "Saturation";
    ui_type = "slider";
    ui_min = 0.0; ui_max = 2.0; ui_step = 0.1;
> = 1.0;

uniform float SharpenAmount <
    ui_label = "Sharpness";
    ui_type = "slider";
    ui_min = 0.0; ui_max = 2.0; ui_step = 0.05;
> = 0.0;

sampler2D ReShade_BackBuffer { Texture = ReShade::BackBufferTex; };

float4 Scanline_CAPS(float4 vpos : SV_Position, float2 texcoord : TEXCOORD) : SV_Target
{
    float2 source_size = float2(BUFFER_WIDTH, BUFFER_HEIGHT);
    
    // Amostragem direta um para um da resolução corrente (960x540)
    float2 sample_coord = texcoord;

    float2 texelSize = float2(1.0, 1.0) / source_size;
    float2 ca_offset = float2(CA_Amount * texelSize.x, 0.0);

    float r = tex2D(ReShade_BackBuffer, sample_coord - ca_offset).r;
    float g = tex2D(ReShade_BackBuffer, sample_coord).g;
    float b = tex2D(ReShade_BackBuffer, sample_coord + ca_offset).b;
    float a = tex2D(ReShade_BackBuffer, sample_coord).a;

    float4 color = float4(r, g, b, a);

    // Nitidez (Sharpening)
    if (SharpenAmount > 0.0)
    {
        float3 neighbor_blur = tex2D(ReShade_BackBuffer, sample_coord + float2(-texelSize.x, 0.0)).rgb;
        neighbor_blur += tex2D(ReShade_BackBuffer, sample_coord + float2(texelSize.x, 0.0)).rgb;
        neighbor_blur += tex2D(ReShade_BackBuffer, sample_coord + float2(0.0, -texelSize.y)).rgb;
        neighbor_blur += tex2D(ReShade_BackBuffer, sample_coord + float2(0.0, texelSize.y)).rgb;
        neighbor_blur *= 0.25;

        color.rgb = color.rgb + (color.rgb - neighbor_blur) * SharpenAmount;
    }

    color.rgb += Brightness;
    color.rgb = (color.rgb - float3(0.5, 0.5, 0.5)) * Contrast + float3(0.5, 0.5, 0.5);
    float luma = dot(color.rgb, float3(0.2126, 0.7152, 0.0722));
    color.rgb = lerp(float3(luma, luma, luma), color.rgb, Saturation);

    // Scanlines aplicadas estritamente nas linhas do buffer de 540p
    if (EnableScanlines)
    {
        // vpos.y varia de 0 a 539. 
        // As linhas ímpares internas do buffer receberão a cor preta.
        if (frac(floor(vpos.y) * 0.5) > 0.0)
        {
            color.rgb = lerp(color.rgb, float3(0.0, 0.0, 0.0), Darkness);
        }
    }

    return color;
}

technique Scanline_CA
{
    pass
    {
        VertexShader = PostProcessVS;
        PixelShader = Scanline_CAPS;
    }
}